package org.pentaho.cubic.client.perspective.tree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.pentaho.cubic.client.dialogs.NotificationDialog;
import org.pentaho.cubic.client.perspective.browser.IFileSummary;
import org.pentaho.cubic.client.perspective.filedetails.CubicFileDetails;
import org.pentaho.cubic.client.perspective.fileproperties.FileItem;
import org.pentaho.cubic.client.perspective.tabs.CubicTabLayoutPanel;
import org.pentaho.cubic.client.solution.ISolutionDocumentListener;
import org.pentaho.cubic.client.solution.SolutionManager;
import org.pentaho.cubic.client.utils.ElementUtils;
import org.pentaho.cubic.client.utils.StringTokenizer;
import org.pentaho.cubic.client.utils.StringUtils;
import org.pentaho.cubic.client.perspective.tabs.CubicTabLayoutPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.debug.client.DebugInfo;
import com.google.gwt.event.logical.shared.OpenHandler;

import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;

public class CubicTree extends Tree implements ISolutionDocumentListener{
	private boolean showLocalizedFileNames = true;
	private boolean showHiddenFiles = false;
	private Document solutionDocument;
	private boolean isAdministrator = false;
	private boolean createRootNode = false;
	private boolean useDescriptionsForTooltip = true;

	private FileTreeItem selectedItem = null;

	//FocusPanel focusable = new FocusPanel();

	public CubicTree() {
		//super(MantleImages.images, true);
		super();
		
		setAnimationEnabled(true);
		sinkEvents(Event.ONDBLCLICK);
		// popupMenu.setAnimationEnabled(false);
		
		DOM.setElementAttribute(getElement(), "oncontextmenu", "return false;"); //$NON-NLS-1$ //$NON-NLS-2$

		/*
		DOM.setStyleAttribute(focusable.getElement(), "fontSize", "0"); //$NON-NLS-1$ //$NON-NLS-2$
		DOM.setStyleAttribute(focusable.getElement(), "position", "absolute"); //$NON-NLS-1$ //$NON-NLS-2$
		DOM.setStyleAttribute(focusable.getElement(), "outline", "0px"); //$NON-NLS-1$ //$NON-NLS-2$
		DOM.setStyleAttribute(focusable.getElement(), "width", "1px"); //$NON-NLS-1$ //$NON-NLS-2$
		DOM.setStyleAttribute(focusable.getElement(), "height", "1px"); //$NON-NLS-1$ //$NON-NLS-2$
		DOM.setElementAttribute(focusable.getElement(), "hideFocus", "true"); //$NON-NLS-1$ //$NON-NLS-2$
		DOM.setIntStyleAttribute(focusable.getElement(), "zIndex", -1); //$NON-NLS-1$
		DOM.appendChild(getElement(), focusable.getElement());
		DOM.sinkEvents(focusable.getElement(), Event.FOCUSEVENTS);
		*/
		
		// By default, expanding a node does not select it. Add that in here
		this.addOpenHandler(new OpenHandler<TreeItem>() {
			public void onOpen(OpenEvent<TreeItem> event) {
				CubicTree.this.setSelectedItem(event.getTarget());
			}
		});

		getElement().setId("solutionTree");
		//getElement().getStyle().setProperty("marginTop", "29px"); //$NON-NLS-1$ //$NON-NLS-2$
		
		SolutionManager.getInstance().addSolutionDocumentListener(this);
		//UserSettingsManager.getInstance().addUserSettingsListener(this);
	}
/*
	public void onFetchUserSettings(ArrayList<IUserSetting> settings) {
		if (settings == null) {
			return;
		}

		for (IUserSetting setting : settings) {
			if (IMantleUserSettingsConstants.MANTLE_SHOW_LOCALIZED_FILENAMES.equals(setting.getSettingName())) {
				boolean showLocalizedFileNames = "true".equals(setting.getSettingValue()); //$NON-NLS-1$
				setShowLocalizedFileNames(showLocalizedFileNames);
			} else if (IMantleUserSettingsConstants.MANTLE_SHOW_DESCRIPTIONS_FOR_TOOLTIPS.equals(setting.getSettingName())) {
				boolean useDescriptions = "true".equals(setting.getSettingValue()); //$NON-NLS-1$
				setUseDescriptionsForTooltip(useDescriptions);
			} else if (IMantleUserSettingsConstants.MANTLE_SHOW_HIDDEN_FILES.equals(setting.getSettingName())) {
				boolean showHiddenFiles = "true".equals(setting.getSettingValue()); //$NON-NLS-1$
				setShowHiddenFiles(showHiddenFiles);
			}
		}

		SolutionBrowserPerspective.getInstance().updateViewMenu();
	}
*/

	public void onBrowserEvent(Event event) {
		int eventType = DOM.eventGetType(event);
		
		switch (eventType) {
		case Event.ONMOUSEDOWN:
		case Event.ONMOUSEUP:
		case Event.ONCLICK: {
			try {
				int[] scrollOffsets = ElementUtils.calculateScrollOffsets(getElement());
				int[] offsets = ElementUtils.calculateOffsets(getElement());
				//DOM.setStyleAttribute(focusable.getElement(), "top", (event.getClientY() + scrollOffsets[1] - offsets[1]) + "px"); //$NON-NLS-1$ //$NON-NLS-2$
			} catch (Exception ignored) {
				// ignore any exceptions fired by this. Most likely a result of the element
				// not being on the DOM
			}
			break;
		}
		}

		try {
			if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
				/*
				// load menu (Note: disabled as Delete and Properties have no meaning for Folders now
				int left = Window.getScrollLeft() + DOM.eventGetClientX(event);
				int top = Window.getScrollTop() + DOM.eventGetClientY(event);
				//final PopupPanel popupMenu = MantlePopupPanel.getInstance(true);
				popupMenu.setPopupPosition(left, top);
				MenuBar menuBar = new MenuBar(true);
				menuBar.setAutoOpen(true);
				//menuBar.addItem(new MenuItem(Messages.getString("createNewFolderEllipsis"), new FileCommand(FileCommand.COMMAND.CREATE_FOLDER, popupMenu, getFileSummary())));
				menuBar.addItem("Delete", new FileCommand(FileCommand.COMMAND.DELETE, popupMenu, getFileSummary()))); //$NON-NLS-1$
				menuBar.addSeparator();
				menuBar.addItem("Properties", new FileCommand(FileCommand.COMMAND.PROPERTIES, popupMenu, getFileSummary()))); //$NON-NLS-1$
				popupMenu.setWidget(menuBar);
				popupMenu.hide();
				popupMenu.show();
				*/
				NotificationDialog dlx = new NotificationDialog("Solution Tree", "Right Click");
				dlx.center();
			} else if (DOM.eventGetType(event) == Event.ONCLICK) {
				FileTreeItem selected = (FileTreeItem) getSelectedItem();
				CubicFileDetails.getInstance().setFileDetails(selected.getFile());
				//CubicTabLayoutPanel.getInstance().addTab(selected.getFileName(), selected.getDescription(), selected.getURL());
				
			} else if (DOM.eventGetType(event) == Event.ONDBLCLICK) {
				getSelectedItem().setState(!getSelectedItem().getState(), true);
				FileTreeItem selected = (FileTreeItem) getSelectedItem();
				if (selected.isDirectory() == false) {
					CubicTabLayoutPanel.getInstance().addTab(selected.getText(), selected.getText() + ": " + selected.getDescription(), selected.getURL());
				}
			} else {
				super.onBrowserEvent(event);
			}
		} catch (Throwable t) {
			// death to this browser event
		}
		TreeItem selItem = getSelectedItem();
		if (selItem != null) {
			DOM.scrollIntoView(selItem.getElement());
		}
	}

	public void beforeFetchSolutionDocument() {
		if (getSelectedItem() != null) {
			selectedItem = (FileTreeItem) getSelectedItem();
		}
		clear();
		addItem(new TreeItem("Loading...")); //$NON-NLS-1$
	}

	public void onFetchSolutionDocument(Document solutionDocument) {
		if (solutionDocument == null) {
			NotificationDialog dlx = new NotificationDialog("Solution Tree", "Null Exception");
			dlx.center();
			return;
		}
		this.solutionDocument = solutionDocument;
		// remember selectedItem, so we can reselect it after the tree is loaded
		clear();
		// get document root item
		Element solutionRoot = solutionDocument.getDocumentElement();
		//addItem(new TreeItem("Check Point"));
		if (createRootNode) {
			FileTreeItem rootItem = new FileTreeItem();
			rootItem.setText(solutionRoot.getAttribute("path")); //$NON-NLS-1$
			rootItem.setTitle(solutionRoot.getAttribute("path")); //$NON-NLS-1$
			rootItem.getElement().setId(solutionRoot.getAttribute("path"));
			ElementUtils.killAllTextSelection(rootItem.getElement());

			// added so we can traverse the true names
			rootItem.setFileName("/"); //$NON-NLS-1$
			addItem(rootItem);
			buildSolutionTree(rootItem, solutionRoot);
		} else {
			buildSolutionTree(null, solutionRoot);
			// sort the root elements
			ArrayList<TreeItem> roots = new ArrayList<TreeItem>();
			for (int i = 0; i < getItemCount(); i++) {
				roots.add(getItem(i));
			}
			Collections.sort(roots, new Comparator<TreeItem>() {
				public int compare(TreeItem o1, TreeItem o2) {
					return o1.getText().compareTo(o2.getText());
				}
			});
			clear();
			for (TreeItem myRootItem : roots) {
				addItem(myRootItem);
			}
		}
		if (selectedItem != null) {
			ArrayList<FileTreeItem> parents = new ArrayList<FileTreeItem>();
			while (selectedItem != null) {
				parents.add(selectedItem);
				selectedItem = (FileTreeItem) selectedItem.getParentItem();
			}
			Collections.reverse(parents);
			selectFromList(parents);
		} else {
			for (int i = 0; i < getItemCount(); i++) {
				((FileTreeItem) getItem(i)).setState(true);
			}
		}
	}

	public ArrayList<FileTreeItem> getAllNodes() {
		ArrayList<FileTreeItem> nodeList = new ArrayList<FileTreeItem>();
		for (int i = 0; i < this.getItemCount(); i++) {
			nodeList.add((FileTreeItem) this.getItem(i));
			getAllNodes((FileTreeItem) this.getItem(i), nodeList);
		}
		return nodeList;
	}

	private void getAllNodes(FileTreeItem parent, ArrayList<FileTreeItem> nodeList) {
		for (int i = 0; i < parent.getChildCount(); i++) {
			FileTreeItem child = (FileTreeItem) parent.getChild(i);
			nodeList.add(child);
			getAllNodes(child, nodeList);
		}
	}

	public ArrayList<String> getPathSegments(String path) {
		ArrayList<String> pathSegments = new ArrayList<String>();
		if (path != null) {
			if (path.startsWith("/")) { //$NON-NLS-1$
				path = path.substring(1);
			}
			StringTokenizer st = new StringTokenizer(path, '/');
			for (int i = 0; i < st.countTokens(); i++) {
				pathSegments.add(st.tokenAt(i));
			}
		}
		return pathSegments;
	}

	/**
	 * Checks if the given file name exists in the directory specified by pathSegments
	 * 
	 * @param pathSegments
	 *          List consisting of hierarchial names of directory {a/b/c/example.txt => [a,b,c]}
	 * @param pFileName
	 *          File name to be looked for in the given directory {a/b/c/example.txt => example.txt}
	 * @return True if file exists, false otherwise
	 */
	public boolean doesFileExist(final ArrayList<String> pathSegments, final String pFileName) {
		// The IF part is to check if we are looking only at the top most level
		// If so then we need to iterate through itemCount
		if (pathSegments.size() == 0) {
			final int itemCount = getItemCount();
			for (int x = 0; x < itemCount; x++) {
				final FileTreeItem selectedItem = (FileTreeItem) getItem(x);
				if (selectedItem.getFileName().equalsIgnoreCase(pFileName)) {
					return true;
				}
			}
		} else {
			// If we are here then we are looking for a file inside a sub directory in the solution tree
			// getTreeItem method returns us the directory node we are looking for based on the pathSegments variable
			final FileTreeItem directoryItem = getTreeItem(pathSegments);

			if (directoryItem != null) {
				// Iterate through the directory and check if the name we are searching for exists in
				// the file list of current dir
				final ArrayList<Element> filesInCurrDirectory = (ArrayList<Element>) directoryItem.getUserObject();
				if (filesInCurrDirectory != null) {
					for (Element fileElement : filesInCurrDirectory) {
						final String currentFileName = fileElement.getAttribute("name"); //$NON-NLS-1$
						if ((currentFileName != null) && (currentFileName.equalsIgnoreCase(pFileName))) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	public FileTreeItem getTreeItem(final ArrayList<String> pathSegments) {
		if (pathSegments.size() > 0) {
			// the first path segment is going to be a 'root' in the tree
			String rootSegment = pathSegments.get(0);
			for (int i = 0; i < getItemCount(); i++) {
				FileTreeItem root = (FileTreeItem) getItem(i);
				if (root.getFileName().equalsIgnoreCase(rootSegment)) {
					ArrayList<String> tmpPathSegs = (ArrayList<String>) pathSegments.clone();
					tmpPathSegs.remove(0);
					return getTreeItem(root, tmpPathSegs);
				}
			}
		}
		return null;
	}

	private FileTreeItem getTreeItem(final FileTreeItem root, final ArrayList<String> pathSegments) {
		int depth = 0;
		FileTreeItem currentItem = root;
		while (depth < pathSegments.size()) {
			String pathSegment = pathSegments.get(depth);
			for (int i = 0; i < currentItem.getChildCount(); i++) {
				FileTreeItem childItem = (FileTreeItem) currentItem.getChild(i);
				if (childItem.getFileName().equalsIgnoreCase(pathSegment)) {
					currentItem = childItem;
				}
			}
			depth++;
		}
		// let's check if the currentItem matches our segments (it might point to the last item before
		// we eventually failed to find the complete match)
		FileTreeItem tmpItem = currentItem;
		depth = pathSegments.size()-1;
		while (tmpItem != null && depth >= 0) {
			if (tmpItem.getFileName().equalsIgnoreCase(pathSegments.get(depth))) {
				tmpItem = (FileTreeItem) tmpItem.getParentItem();
				depth--;
			} else {
				// every item must match
				return null;
			}
		}

		return currentItem;
	}

	private void selectFromList(ArrayList<FileTreeItem> parents) {
		FileTreeItem pathDown = null;
		for (int i = 0; i < parents.size(); i++) {
			FileTreeItem parent = parents.get(i);
			if (pathDown == null) {
				for (int j = 0; j < getItemCount(); j++) {
					FileTreeItem possibleItem = (FileTreeItem) getItem(j);
					if (parent.getFileName().equals(possibleItem.getFileName())) {
						pathDown = possibleItem;
						pathDown.setState(true, true);
						pathDown.setSelected(true);
						break;
					}
				}
			} else {
				for (int j = 0; j < pathDown.getChildCount(); j++) {
					FileTreeItem possibleItem = (FileTreeItem) pathDown.getChild(j);
					if (parent.getFileName().equals(possibleItem.getFileName())) {
						pathDown = possibleItem;
						pathDown.setState(true, true);
						break;
					}
				}
			}
		}
		if (pathDown != null) {
			setSelectedItem(pathDown);
			pathDown.setState(true, true);
		}
	}

	@SuppressWarnings("unchecked")//$NON-NLS-1$
	private void buildSolutionTree(FileTreeItem parentTreeItem, Element parentElement) {
		NodeList children = parentElement.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Element childElement = (Element) children.item(i);
			//if (childElement == this.focusable) {
			//	continue;
			//}
			boolean isVisible = "true".equals(childElement.getAttribute("visible")); //$NON-NLS-1$ //$NON-NLS-2$
			boolean isDirectory = "true".equals(childElement.getAttribute("isDirectory")); //$NON-NLS-1$ //$NON-NLS-2$
			if (isVisible || showHiddenFiles) {
				String fileName = childElement.getAttribute("name"); //$NON-NLS-1$
				String localizedName = childElement.getAttribute("localized-name"); //$NON-NLS-1$
				String description = childElement.getAttribute("description"); //$NON-NLS-1$
				FileTreeItem childTreeItem = new FileTreeItem();

				String id = null;
				Element parent = childElement;
				while (parent != null) {
					if (StringUtils.isEmpty(parent.getAttribute("name"))) {
						try {
							parent = (Element) parent.getParentNode();
						} catch (Throwable t) {
							parent = null;
						}
						continue;
					}
					if (id != null) {
						id = parent.getAttribute("name") + "/" + id;
					} else {
						id = parent.getAttribute("name");
					}
					if (parent.getParentNode() == null) {
						break;
					}
					try {
						parent = (Element) parent.getParentNode();
					} catch (Throwable t) {
						parent = null;
					}
				}
				childTreeItem.getElement().setAttribute("id", id);

				ElementUtils.killAllTextSelection(childTreeItem.getElement());
				
				childTreeItem.setURL(childElement.getAttribute("url")); //$NON-NLS-1$
				childTreeItem.setFileName(fileName);
				childTreeItem.setDescription(description);
				childTreeItem.isDirectory(isDirectory);				
				
				if (showLocalizedFileNames) {
					//childTreeItem.setText(localizedName);
					if (childTreeItem.isDirectory())
						childTreeItem.setHTML("<b>" + localizedName + "</b>");
					else
						childTreeItem.setText(localizedName);
					if (isUseDescriptionsForTooltip() && !StringUtils.isEmpty(description)) {
						childTreeItem.setTitle(description);
					} else {
						childTreeItem.setTitle(fileName);
					}
				} else {
					childTreeItem.setText(fileName);
					if (isUseDescriptionsForTooltip() && !StringUtils.isEmpty(description)) {
						childTreeItem.setTitle(description);
					} else {
						childTreeItem.setTitle(localizedName);
					}
				}
				
				//if (parentTreeItem == null) {
				if (parentTreeItem == null && isDirectory) {
					addItem(childTreeItem);
				} else {

					try {
						// find the spot in the parentTreeItem to insert the node (based on showLocalizedFileNames)
						if (parentTreeItem.getChildCount() == 0) {
							parentTreeItem.addItem(childTreeItem);
						} else {
							// this does sorting
							boolean inserted = false;
							for (int j = 0; j < parentTreeItem.getChildCount(); j++) {
								FileTreeItem kid = (FileTreeItem) parentTreeItem.getChild(j);
								if (showLocalizedFileNames) {
									if (childTreeItem.getText().compareTo(kid.getText()) <= 0) {
										// leave all items ahead of the insert point
										// remove all items between the insert point and the end
										// add the new item
										// add back all removed items
										ArrayList<FileTreeItem> removedItems = new ArrayList<FileTreeItem>();
										for (int x = j; x < parentTreeItem.getChildCount(); x++) {
											FileTreeItem removedItem = (FileTreeItem) parentTreeItem.getChild(x);
											removedItems.add(removedItem);
										}
										for (FileTreeItem removedItem : removedItems) {
											parentTreeItem.removeItem(removedItem);
										}
										parentTreeItem.addItem(childTreeItem);
										inserted = true;
										for (FileTreeItem removedItem : removedItems) {
											parentTreeItem.addItem(removedItem);
										}
										break;
									}
								} else {
									parentTreeItem.addItem(childTreeItem);
									inserted = true;
								}
							}
							if (!inserted) {
								parentTreeItem.addItem(childTreeItem);
							}
						}
					} catch (Exception e) { /* Error with FF */
					}
				}
				FileTreeItem tmpParent = childTreeItem;
				String pathToChild = tmpParent.getFileName();
				while (tmpParent.getParentItem() != null) {
					tmpParent = (FileTreeItem) tmpParent.getParentItem();
					pathToChild = tmpParent.getFileName() + "/" + pathToChild; //$NON-NLS-1$
				}

				if (parentTreeItem != null) {
					ArrayList<Element> files = (ArrayList<Element>) parentTreeItem.getUserObject();
					if (files == null) {
						files = new ArrayList<Element>();
						parentTreeItem.setUserObject(files);
					}
					files.add(childElement);
					childTreeItem.setFile(getFileItem(childElement));
				}

				if (isDirectory) {
					buildSolutionTree(childTreeItem, childElement);
				} else {
					// Cubic MOD
					//if (parentTreeItem != null) {
					//	parentTreeItem.removeItem(childTreeItem);
					//}
				}
			}
		}
	}


	public void setShowLocalizedFileNames(boolean showLocalizedFileNames) {
		this.showLocalizedFileNames = showLocalizedFileNames;
		// use existing tree and switch text/title
		for (int i = 0; i < getItemCount(); i++) {
			toggleLocalizedFileNames((FileTreeItem) getItem(i));
		}
	}

	private void toggleLocalizedFileNames(FileTreeItem parentTreeItem) {
		String title = parentTreeItem.getTitle();
		String text = parentTreeItem.getText();
		parentTreeItem.setTitle(text);
		parentTreeItem.setText(title);
		for (int i = 0; i < parentTreeItem.getChildCount(); i++) {
			toggleLocalizedFileNames((FileTreeItem) parentTreeItem.getChild(i));
		}
	}

	public String getSolution() {
		// http://localhost:8080/pentaho/ViewAction?solution=samples&path=reporting&action=JFree_XQuery_report.xaction
		// the solution part of the url
		for (int i = 0; i < getItemCount(); i++) {
			if (getSelectedItem() == getItem(i)) {
				return ((FileTreeItem) getItem(i)).getFileName();
			}
		}
		FileTreeItem tmpParent = (FileTreeItem) getSelectedItem();
		ArrayList<FileTreeItem> parents = new ArrayList<FileTreeItem>();
		while (tmpParent != null) {
			parents.add(tmpParent);
			tmpParent = (FileTreeItem) tmpParent.getParentItem();
		}
		// if each solution is a root, then 1st item is solution
		// else solution is 2nd item
		return parents.get(parents.size() - (isCreateRootNode() ? 2 : 1)).getFileName();
	}

	public String getPath() {
		// http://localhost:8080/pentaho/ViewAction?solution=samples&path=reporting&action=JFree_XQuery_report.xaction
		// the path part of the url

		// if we've selected a root level node, we're at the root of a solution, so return "/"
		for (int i = 0; i < getItemCount(); i++) {
			if (getSelectedItem() == getItem(i)) {
				// return ((FileTreeItem) getItem(i)).getFileName();
				return "/"; //$NON-NLS-1$
			}
		}

		FileTreeItem tmpParent = (FileTreeItem) getSelectedItem();
		ArrayList<FileTreeItem> parents = new ArrayList<FileTreeItem>();
		while (tmpParent != null) {
			parents.add(tmpParent);
			tmpParent = (FileTreeItem) tmpParent.getParentItem();
		}
		// if each solution is a root, then last item is solution
		// else solution is 2nd-to-last item
		// so we start from either of these positions
		String path = ""; //$NON-NLS-1$
		for (int i = (parents.size()-1) - (isCreateRootNode() ? 2 : 1); i >= 0; i--) {
			FileTreeItem parent = parents.get(i);
			path += "/" + parent.getFileName(); //$NON-NLS-1$
		}
		return path;
	}

	public boolean isShowHiddenFiles() {
		return showHiddenFiles;
	}

	public void setShowHiddenFiles(boolean showHiddenFiles) {
		this.showHiddenFiles = showHiddenFiles;
		onFetchSolutionDocument(solutionDocument);
	}

	public boolean isShowLocalizedFileNames() {
		return showLocalizedFileNames;
	}

	public boolean isUseDescriptionsForTooltip() {
		return useDescriptionsForTooltip;
	}

	public void setUseDescriptionsForTooltip(boolean useDescriptionsForTooltip) {
		this.useDescriptionsForTooltip = useDescriptionsForTooltip;
		onFetchSolutionDocument(solutionDocument);
	}

	public boolean isAdministrator() {
		return isAdministrator;
	}

	public void setAdministrator(boolean isAdministrator) {
		this.isAdministrator = isAdministrator;
	}

	public boolean isCreateRootNode() {
		return createRootNode;
	}

	/*
	Focusable getFocusable() {
		return this.focusable;
	}
	*/
	
	public IFileSummary getFileSummary() {
		final FileTreeItem selectedTreeItem = (FileTreeItem) getSelectedItem();
		if (selectedTreeItem == null) {
			return null;
		}
		String tmpPath = getPath();
		if (!"".equals(getPath())) {
			tmpPath = tmpPath.substring(0, tmpPath.lastIndexOf("/")); //$NON-NLS-1$
		}
		final String path = tmpPath;
		final String solution = getSolution();
		IFileSummary fileSummary = new IFileSummary() {

			public String getLocalizedName() {
				return selectedTreeItem != null ? selectedTreeItem.getText() : null;  
			}

			public String getName() {
				return selectedTreeItem != null ? selectedTreeItem.getFileName() : null;  
			}

			public String getPath() {
				return path;  
			}

			public String getSolution() {
				return solution;  
			}

		};
		return fileSummary;
	}

	private FileItem getFileItem(Element childElement) {
		
		
		String name = childElement.getAttribute("name");
		String solution = "";//getSolution();
		String path = "";//getPath();
		String lastModifiedDateStr = childElement.getAttribute("lastModifiedDate");
		String url = childElement.getAttribute("url");
		String localizedName = childElement.getAttribute("localized-name");
		String description = childElement.getAttribute("description");
		boolean directory = "true".equals(childElement.getAttribute("isDirectory"));
		
		FileItem file = new FileItem(name, localizedName, description, solution, path, lastModifiedDateStr, url, directory);
		
		return file;
	}

}
